﻿#include "passwordsetting.h"
#include "ui_passwordsetting.h"
#include <QDir>
#include <QSettings>
#include <QMessageBox>
PasswordSetting::PasswordSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PasswordSetting)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
}

PasswordSetting::~PasswordSetting()
{
    delete ui;
}

void PasswordSetting::on_savePassword_Btn_clicked()
{
    QString originalPassword = ui->originalPassword_lineEdit->text();
    QString dirPath = "./DependFile/conf/";
    QDir confdir(dirPath);
    if(!confdir.exists())
        confdir.mkdir(dirPath);
    QString confFile(dirPath + "IDconf.ini");
    QSettings iniSetting(confFile, QSettings::IniFormat);
    QString password = iniSetting.value("password").toString();
    if(password != originalPassword)
    {
        ui->originalPasswordTips_Label->setText(tr("原密码输入错误"));
        ui->originalPasswordTips_Label->setVisible(true);
        return;
    }

    if(ui->newPassword_lineEdit->text() != ui->confirmPassword_LineEdit->text())
    {
        ui->confirmPasswordTips_Label->setText(tr("两次输入的密码不同"));
        ui->confirmPasswordTips_Label->setVisible(true);
        return;
    }

    iniSetting.setValue("password",ui->confirmPassword_LineEdit->text());

    ui->confirmPasswordTips_Label->setVisible(false);
    ui->originalPasswordTips_Label->setVisible(false);
    ui->confirmPassword_LineEdit->clear();
    ui->newPassword_lineEdit->clear();
    ui->originalPassword_lineEdit->clear();

    QMessageBox::warning(NULL,tr("提示"),tr("密码更新成功"));
}

void PasswordSetting::on_cancel_Btn_clicked()
{
    ui->originalPassword_lineEdit->clear();
    ui->newPassword_lineEdit->clear();
    ui->confirmPassword_LineEdit->clear();
}

void PasswordSetting::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}
